// 实现这个项目的构建任务

// dest 目标目录
// src 源文件目录
// series 串行任务，传入数组，按顺序依次执行。
// parallel 并行任务，传入数组，同时执行
// watch 监听文件改变，并触犯指定任务
const { dest, src, series, parallel, watch } = require('gulp')

// 自动安装所需要的插件
const loadPlugins = require('gulp-load-plugins')

// 热更新
const browserSync = require('browser-sync')

// 清除文件
const del = require('del')

const plugin = loadPlugins()
const bs = browserSync.create()

const data = {
    menus: [
        {
            name: 'Home',
            icon: 'aperture',
            link: 'index.html'
        },
        {
            name: 'Features',
            link: 'features.html'
        },
        {
            name: 'About',
            link: 'about.html'
        },
        {
            name: 'Contact',
            link: '#',
            children: [
                {
                    name: 'Twitter',
                    link: 'https://twitter.com/w_zce'
                },
                {
                    name: 'About',
                    link: 'https://weibo.com/zceme'
                },
                {
                    name: 'divider'
                },
                {
                    name: 'About',
                    link: 'https://github.com/zce'
                }
            ]
        }
    ],
    pkg: require('./package.json'),
    date: new Date()
}

// 删除目标文件和临时文件
const clean = () => {
    return del(['dist', 'temp'])
}

const style = () => {
    /**
     * 没有src一样的路径。所以需要指定 {base：'src'}
     * 以 src 为根目录，通配 src/assets/styles 下的所有 scss 文件
     */
    return src('src/assets/styles/*.scss', { base: 'src' }).pipe(plugin.sass())
        .pipe(dest('temp'))
        .pipe(bs.reload({ stream: true }))
}

/**
 * babel 是一个 新特性转换的平台，没有实际的转换作用
 * 所以需要指定核心转换库： preset-env
 */
const script = () => {
    return src('src/assets/scripts/*.js', { base: 'src' }).pipe(plugin.babel({
        presets: ['@babel/preset-env']
    }))
        .pipe(dest('temp'))
        .pipe(bs.reload({ stream: true }))
}



const image = () => {
    return src("src/assets/images/**", { base: 'src' }).pipe(plugin.imagemin()).pipe(dest('dist'))
}

const page = () => {
    return src("src/*.html", { base: 'src' }).pipe(plugin.swig({ data })).pipe(dest('temp')).pipe(bs.reload({ stream: true }))
}

/**
 * plugin.imagemin()
 * 能压缩就压缩，不能压缩就不压缩。这里是为了 压缩 svg
 */
const fonts = () => {
    return src("src/assets/fonts/**", { base: 'src' }).pipe(plugin.imagemin()).pipe(dest('dist'))
}

const extra = () => {
    return src("public/**", { base: 'public' }).pipe(dest('dist'))
}

const serve = () => {
    watch("src/assets/styles/*.scss", style)
    watch("src/assets/scripts/*.js", script)
    watch("src/*.html", page)
    watch([
        'src/assets/images/**',
        'src/assets/scripts/*.js',
        'src/*.html'
    ], bs.reload)

    bs.init({
        server: {
            port: 8080,
            // baseDir: 'dist',
            baseDir: ["temp", "src", "public"],  // 开发阶段不需要公共文件，可以减少构建过程
            // files: 'dist/**',
            // open:false,
            routes: {
                '/node_modules': "node_modules"
            }
        }
    })
}

const useref = () => {
    return src('temp/*.html', { base: 'temp' })
        .pipe(plugin.useref({ searchPath: ['temp', '.'] }))
        .pipe(plugin.if(/\.js$/, plugin.uglify()))
        .pipe(plugin.if(/\.css$/, plugin.cleanCss()))
        .pipe(plugin.if(/\.html$/, plugin.htmlmin({ collapseWhitespace: true, minifyCSS: true, minifyJS: true })))
        .pipe(dest('dist'))
}
const compile = parallel(page, script, style)

const build = series(clean, parallel(series(compile, useref), fonts, image, extra))

const develop = series(compile, serve)

module.exports = {
    clean,
    build,
    start: develop,
    serve
}